package com.sangmo.fts.teacher.calculator;

import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.data.json.context.KeyValue;
import com.sangmo.boot.framework.data.json.load.Calculator;
import com.sangmo.fts.teacher.model.Examination;
import com.sangmo.fts.teacher.repo.ExaminationRepo;
import com.sangmo.fts.training.model.Teammate;
import com.sangmo.fts.training.model.Training;
import com.sangmo.fts.training.repo.TeamRepo;
import com.sangmo.fts.training.repo.TeammateRepo;
import com.sangmo.fts.training.repo.TrainingRepo;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class ExaminationTotalNumberCalculator implements Calculator<Examination>, Examination.ITotalNumberCalculator {

    @Override
    public Object calc(Examination bean, Collection<KeyValue> exts) {

        if (Examination.Modes.INDIVIDUAL.equals(bean.getMode())) {
            TrainingRepo trainingRepo = ApplicationUtils.getBean(TrainingRepo.class);
            List<Training> trainingList = trainingRepo.findByExaminationId(bean.getId());
            return trainingList.size();
        } else if (Examination.Modes.TEAM.equals(bean.getMode())){
            TeammateRepo teammateRepo = ApplicationUtils.getBean(TeammateRepo.class);
            int count = teammateRepo.countByExaminationId(bean.getId());
            return count;
        }
        return null;
    }
}
